
import { ModuleGraph } from './ModuleGraph';
import { ServerContext } from "./server/index";
import { blue, green } from "picocolors";
import { getShortName, slash } from "./utils";
export function bindingHMREvents(serverContext: ServerContext) {
  const { ws, watcher, root } = serverContext
  watcher.on("change", async (file) => {
    console.log(`✨${blue("[hmr]")} ${green(file)} changed`);

    const { moduleGraph } = serverContext;
    await moduleGraph.invalidateModule(file);

    ws.send({
      type: "update",
      updates: [
        {
          type: "js-update",
          timestamp: Date.now(),
          path: "/" + getShortName(slash(file), root),
          acceptedPath: "/" + getShortName(slash(file), root),
        }
      ]
    })
  })
}